package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EcGaCredentialRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.ga.EcGaCredential;
import com.ndp.fb.rdb.model.ga.EcGaCredentialExt;

import java.util.Date;

/**
 * Created by infi.he on 2016/4/13.
 */
@Service
public class EcGaCredentialRdbServiceImpl extends DaoTemplate implements EcGaCredentialRdbService {

    @Override
    public Pager<EcGaCredentialExt> listEcGaCredentialExt(Long orgId,Integer firstResult, Integer sizeNo) {
        String countSsql = "select count(1) cnt from (SELECT pm.* from product_manage pm LEFT JOIN ec_ga_credential gc on gc.product_id = pm.product_id where pm.org_id = ? and pm.is_delete = 0) as pagertable";
        Object count = getJdbcTemplate().queryForMap(countSsql, new Object[]{orgId}).get("cnt");
        logger.debug("总数 >>>> " + count.toString());
        Pager pager = new Pager();
        pager.setTotalSize(Long.parseLong(count.toString()));
        String sql = "SELECT * from product_manage pm LEFT JOIN ec_ga_credential gc on gc.product_id = pm.product_id where pm.org_id = ? and pm.is_delete = 0 LIMIT ?,?";
        pager.setResultList(getJdbcTemplate().query(sql, new Object[]{orgId,firstResult, sizeNo}, DefaultRowMapper.autoRowMapper(EcGaCredentialExt.class)));
        return pager;
    }

    @Override
    public EcGaCredential findByOrgIdAndProductId(Long orgId, Long productId) {
        String sql = "SELECT * FROM ec_ga_credential where org_id = ? and product_id = ? and status = '1' ";
        return findOne(sql, new Object[]{orgId, productId}, DefaultRowMapper.autoRowMapper(EcGaCredential.class));
    }

    @Override
    public void saveOrUpdate(EcGaCredential gaCredential) {
        String sql = "SELECT * FROM ec_ga_credential where org_id = ? and product_id = ? ";
        EcGaCredential one = findOne(sql, new Object[]{gaCredential.getOrgId(), gaCredential.getProductId()}, DefaultRowMapper.autoRowMapper(EcGaCredential.class));
        if (one == null) {
            saveResultId(gaCredential);
        } else {
            String updateSql = "update ec_ga_credential set access_token = ?, expiration_time_milliseconds = ?,tracking_id= ?,refresh_token = ?," +
                    "user_id = ? ,update_time = ?, status = ?,tracking_id=?,ga_user_name=?  where org_id = ? and product_id = ? ";
            getJdbcTemplate().update(updateSql, new Object[]{gaCredential.getAccessToken(), gaCredential.getExpirationTimeMilliseconds(), gaCredential.getTrackingId(), gaCredential.getRefreshToken(),
                    gaCredential.getUserId(), gaCredential.getUpdateTime(), gaCredential.getStatus(),gaCredential.getTrackingId(),gaCredential.getGaUserName(), gaCredential.getOrgId(), gaCredential.getProductId()});
        }
    }

    @Override
    public void unauth(Long orgId, String aLong) {
        String sql = "update ec_ga_credential set update_time = ? ,status = '3' where  org_id = ? and product_id = ?";
        getJdbcTemplate().update(sql, new Object[]{new Date(),orgId, aLong});
    }
}
